package vn.fsoft.gst.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import vn.fsoft.gst.domain.Department;
import vn.fsoft.gst.domain.Directorate;
import vn.fsoft.gst.domain.Organisation;
import vn.fsoft.gst.service.AddressService;
import vn.fsoft.gst.service.ContactService;
import vn.fsoft.gst.service.DepartmentService;
import vn.fsoft.gst.service.DirectorateService;
import vn.fsoft.gst.service.OrganisationService;
import vn.fsoft.gst.service.SicService;
import vn.fsoft.gst.service.ValidateService;
/**
 *
 *
 * @author MinhNT
 *
 */
@Controller
public class DirectorateController {
    /**
     *
     */
    @Autowired
    private OrganisationService organisationService;
    /**
     *
     */
    @Autowired
    private AddressService addressService;
    /**
     *
     */
    @Autowired
    private ContactService contactService;
    /**
     *
     */
    @Autowired
    private SicService sicService;
    /**
     *
     */
    @Autowired
    private DirectorateService directorateService;
    /**
     *
     */
    @Autowired
    private DepartmentService departmentService;
    /**
     *
     */
    @Autowired
    private ValidateService validateService;
    /**
     *
     * @param model
     * parameter model send to form jsp
     * @param orgID
     * orgID get from request
     * @return
     * view
     */
    @RequestMapping(value = "/addDirectorate", method = RequestMethod.GET)
    public final String addDirectorate(final ModelMap model,
                    @RequestParam final int orgID) {
        Directorate directorate = new Directorate();
        Organisation  organisation = organisationService
                .getOrganisationByID(orgID);
        directorate.setOrganisation(organisation);
        model.addAttribute("organisation", organisation);
        model.addAttribute("directorate", directorate);
        model.addAttribute("orgID", orgID);
        return "addDirectorate";
    }
    /**
     *
     * @param directorate
     * model directorate get from form
     * @return
     * view
     */
    @RequestMapping(value = "/addDirectorate", method = RequestMethod.POST)
    public final String addDirectorate(@ModelAttribute
                (value = "directorate") final Directorate directorate) {
        Boolean result = validateService.validate(directorate
                .getDirectorateName(), directorate.getContact().getContactID(),
                directorate.getAddress().getAddressID(), directorate
                .getAddressLine1(), directorate.getPhoneNumber(),
                directorate.getDescriptionShort());
        if (!result) {
        return "redirect:addDirectorate?orgID="
                + directorate.getOrganisation().getOrgID();
        } else {
            directorateService.add(directorate);
            return "redirect:orgDetail?orgId="
                    + directorate.getOrganisation().getOrgID();
        }
    }
    /**
     *
     * @param model
     * parameter
     * @param directorateID
     * parameter directorateID of Directorate
     * @param orgID
     * parameter orgID of Organisation
     * @return
     * view
     */
    @RequestMapping(value = "/amendDirectorate", method = RequestMethod.GET)
    public final String editDirectorate(final ModelMap model,
        @RequestParam final int directorateID, @RequestParam final int orgID) {
        Organisation organisation = organisationService.
                getOrganisationByID(orgID);
        model.addAttribute("organisation", organisation);
        Directorate directorate = directorateService
                .getDirectorateById(directorateID);
        String typeOfBussines = sicService.getTypeOfBussiness(directorate
                .getSicCode());
        List<Department> listDepartment = departmentService.
                    getListDepartmentByDirecId(directorateID);
        model.addAttribute("listDepartment", listDepartment);
        model.addAttribute("typeOfBussines", typeOfBussines);
        model.addAttribute("directorate", directorate);
        model.addAttribute("orgID", orgID);
        return "addDirectorate";
    }
    /**
     *
     * @param directorate
     * parameter
     * @param model
     * parameter
     * @param request
     * parameter
     * @param response
     * parameter
     * @param session
     * parameter
     * @return
     * view
     */
    @RequestMapping(value = "/amendDirectorate", method = RequestMethod.POST)
    public final String editDirectorate(
            @ModelAttribute(value = "directorate") final
            Directorate directorate, final ModelMap model,
            final HttpServletRequest request,
            final HttpServletResponse response, final HttpSession session) {
        directorate.setAddress(addressService.getAddressByID(directorate
                .getAddress().getAddressID()));
        directorate.setOrganisation(organisationService
                .getOrganisationByID(directorate.getOrganisation().getOrgID()));
        directorate.setContact(directorate.getContact());
        directorateService.edit(directorate);
        return "redirect:orgDetail?orgId="
                + directorate.getOrganisation().getOrgID();
    }
    /**
     *
     * @param model
     * parameter
     * @param directorateID
     * parameter
     */
    @RequestMapping(value = "/activeDirectorate", method = RequestMethod.GET)
    public final void activeDirectorate(final ModelMap model,
        @RequestParam final int directorateID) {
        directorateService.setActiveDirectorate(directorateID, true);
    }
}
